package com.ruijie.acl.web;

import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclInterface;
import com.ruijie.acl.AclInterfaceView;
import com.ruijie.acl.service.AclInterfaceService;
import com.ruijie.emp.device.Device;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;


/**
 * <p>
 * Title: ACL设备接口管理Action类

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 为ACL设备接口管理模块提供页面数据的显示、获取和页面的跳转。

 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("aclInterfaceAction")
@Scope(ScopeType.CONVERSATION)
public class AclInterfaceAction extends AclBaseAction {

	@In("#{aclInterfaceService}")
	private AclInterfaceService aclInterfaceService;
	
	private ListDataModel interfaceEntityModelList = null;

	private Device device;
	
	private Long deviceId = null;
	
	
	/**
	 * 获取设备接口信息
	 * @param device
	 * @return ListDataModel
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAclInterfaceEntityModelListByDevice(Device device) {
		if(device != null) {
			this.device = device;
			if(interfaceEntityModelList == null || device.getId() != this.deviceId) {
				adjustPage();
				currentPage = aclInterfaceService.pageQueryByDevice(device, page, pageSize);
				interfaceEntityModelList = new ListDataModel((List<AclInterfaceView>)currentPage.getResult());
			}
		}
		return interfaceEntityModelList;
	}

	
	/**
	 * 清除接口In或Out方向的Acl组
	 * @param entity
	 * @param param
	 */
	public void cleanInterfaceAccessGroup(AclInterfaceView entity, String param) {
		if (entity.getAclDeviceId() == null){
			return;
		}
		AclInterface aclInterface = aclInterfaceService.get(entity.getAclDeviceId());
		if (aclInterface != null){
			//清除IN方向
			if(param.equals("IN")) {
				aclInterface.setInAccessGroup(null);
				aclInterface.setInAccessGroupName(null);
			}
			//清除OUT方向
			if(param.equals("OUT")) {
				aclInterface.setOutAccessGroup(null);
				aclInterface.setOutAccessGroupName(null);
			}
			aclInterfaceService.update(aclInterface);
			this.updateModel();
		}
	}
	
	/**
	 * 更新Model
	 * 
	 */
	@Override
	protected void updateModel() {
		interfaceEntityModelList = null;
		getAclInterfaceEntityModelListByDevice(device);
	}
	
	public List<AclInterface> getInterfaceByDevice(Device entity) {
		return aclInterfaceService.getListByDevice(entity);
	}
	@SuppressWarnings("unchecked")
	public List<AclInterfaceView> getInterfaceViewByDevice(Device entity, AccessGroup accessGroup) {
		adjustPage();
		List<AclInterfaceView> list = 
			(List<AclInterfaceView>)(aclInterfaceService.pageQueryByDeviceAndAccessGroup(
					entity, accessGroup, page, pageSize).getResult());
		return list;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return aclInterfaceService;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return null;
	}

	@Override
	protected String getListPage() {
		return null;
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {
		
	}

	@Override
	public void setCriteria(EntityObject criteria) {
		
	}

	@Override
	public void setModel(EntityObject model) {
		
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}
	
	/**
	 * 部署ACL组到接口
	 * @param id
	 * @param device
	 */
	public void reConfig(AclInterfaceView interfaceView,Device device,String InorOut){			
		try {
			aclInterfaceService.reConfig(interfaceView, device, InorOut, "modify");
		} catch (BusinessException e) {
			throw e;
		}
		updateModel();
		addGlobalI18nMessage("acl_device_deploy_success");
	}
	
	
	/**
	 * 删除部署到接口的ACL组

	 * @param id
	 * @param device
	 */
	public void deleteInterfaceConfig(AclInterfaceView interfaceView,Device device,String InorOut){
		try {
			aclInterfaceService.reConfig(interfaceView, device, InorOut, "delete");
		} catch (BusinessException e) {
			throw e;
		}
		aclInterfaceService.removeAclInterfalceByDeviceInterface(interfaceView.getAclDeviceId(),interfaceView.getId(),InorOut);
		updateModel();
		addGlobalI18nMessage("crud_del_success_msg");
	}
	
	
}
